package com.geese.util;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.geese.module.user.model.SecurityUser;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Sets;

public abstract class UserCacheUtil {
	
	private static final int LOGIN_TIMEOUT_SEC = 3600;
	private static Cache<String, SecurityUser> loginUsers = CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(LOGIN_TIMEOUT_SEC, TimeUnit.SECONDS)
			.build();
	private static Set<String> businessLoginIds = Sets.newHashSet();
			
			
	public static void addLoginUser(String token, SecurityUser user) {
		loginUsers.put(token, user);
	}
	
	public static void addBusinessLoginUser(String token, SecurityUser user) {
		businessLoginIds.add(token);
		loginUsers.put(token, user);
	}
	
	public static SecurityUser getLoginUser(String token) {
		return loginUsers.getIfPresent(token);
	}
	
	public static void logout(String token) {
		loginUsers.invalidate(token);
	}
	
	public static boolean checkBusinessUser(String token) {
		return businessLoginIds.contains(token);
	}
	
	public static boolean removeBusinessToken(String token) {
		return businessLoginIds.remove(token);
	}
}
